package com.yhsoft.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.buyer.service.IBuyerOrderService;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.vo.Result;
import com.yhsoft.sys.controller.Response.OrderIdsOutVo;
import com.yhsoft.sys.controller.Response.UserOrderInfoOutVo;
import com.yhsoft.sys.controller.Response.WorkOrderDetailOutVo;
import com.yhsoft.sys.controller.Response.WorkOrderInfoOutVo;
import com.yhsoft.sys.controller.request.AddWorkOrderInVo;
import com.yhsoft.sys.controller.request.DealWorkOrderInVo;
import com.yhsoft.sys.controller.request.SendWorkOrderInVo;
import com.yhsoft.sys.controller.request.WorkOrderQuery;
import com.yhsoft.sys.service.IWorkOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 工单信息表 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-20
 */
@Api(tags = {"W-工单相关接口"})
@RestController
@RequestMapping("/sys/workOrder")
public class WorkOrderInfoController {
    @Resource
    private IWorkOrderInfoService iWorkOrderInfoService;

    @Resource
    private IBuyerOrderService iBuyerOrderService;

    @ApiOperation("获取用户订单编号列表")
    @GetMapping("/orderIdList")
    public Result<OrderIdsOutVo> getUserOrderIds(@RequestParam(value = "customerId", required = false) @ApiParam(value = "用户id") String customerId) {
        OrderIdsOutVo outVo = iBuyerOrderService.getUserOrderIds(customerId);
        return Result.success(outVo);
    }

    @ApiOperation("获取用户订单商品信息")
    @GetMapping("/orderInfo")
    public Result<UserOrderInfoOutVo> getUserOrderInfo(@RequestParam(value = "orderId", required = false) @ApiParam(value = "订单编号") String orderNo) {
        UserOrderInfoOutVo outVo = iBuyerOrderService.getUserOrderInfo(orderNo);
        return Result.success(outVo);
    }

    @ApiOperation("新增工单")
    @PostMapping("/add")
    public Result<?> add(@RequestBody AddWorkOrderInVo inVo) {
        String msg = iWorkOrderInfoService.add(inVo);
        if (msg.equals("success")) {
            return Result.success(msg);
        }
        return Result.fail(msg);
    }

    @ApiOperation("获取工单列表")
    @GetMapping("/list")
    public Result<PageResult<WorkOrderInfoOutVo>> getList(WorkOrderQuery inVo,
                                                          @RequestParam(value = "pageNo") Long pageNo,
                                                          @RequestParam(value = "pageSize") Long pageSize) {
        IPage<WorkOrderInfoOutVo> iPage = iWorkOrderInfoService.getList(inVo, pageNo, pageSize);
        return Result.success(PageResult.valueOf(iPage));
    }

    @ApiOperation("获取工单详情")
    @GetMapping("/detil")
    public Result<WorkOrderDetailOutVo> getWorkOrderdetil(@RequestParam(value = "workOrderId", required = true) @ApiParam(value = "工单ID") String workOrderId) {
        WorkOrderDetailOutVo outVo = iWorkOrderInfoService.getWorkOrderdetil(workOrderId);
        return Result.success(outVo);
    }

    @ApiOperation("分发至供应商/已解决")
    @PostMapping("/send")
    public Result<?> send(@RequestBody SendWorkOrderInVo inVo) {
        String msg = iWorkOrderInfoService.send(inVo);
        if (msg.equals("success")) {
            return Result.success(msg);
        }
        return Result.fail(msg);
    }

    @ApiOperation("立即处理")
    @PostMapping("/deal")
    public Result<?> deal(@RequestBody DealWorkOrderInVo inVo) {
        String msg = iWorkOrderInfoService.deal(inVo);
        if (msg.equals("success")) {
            return Result.success(msg);
        }
        return Result.fail(msg);
    }
}
